<template>
  <c-dialog ref="dialog" :open-fun="openFun" title="变更工程监理">
    <c-list ref="form" :model="formData" :rules="formRules" :column="1" label-width="120px">
      <c-list-item label="门店建设项目号" :value="detail.map(v=>v.constCode)" />
      <c-list-item label="装修任务号" :value="detail.map(v=>v.decorationTaskCode)" />
      <c-list-item label="所属门店" :value="detail.map(v=>v.shopName)" />
      <c-list-item label="变更前工程监理" :value="detail.map(v=>v.proSupervisorName)" />
      <c-list-item label="变更后工程监理" :span="2" prop="proSupervisorCode">
        <c-single-dialog
          ref="proSupervisorCode"
          v-model="formData.proSupervisorCode"
          :label.sync="formData.proSupervisorName"
          :props="{label:'userName',value:'account'}"
          :focus-query="true"
          :multiple="false"
          :config="{
            title:'变更后工程监理',
            width:'600px',
            interfacePath:`/slm-admin/api/v1/user/listUsers`,
            method:'post',
            paramsRaw:{},
            setting:[
              {prop:'account',title:'编码',width:80},
              {prop:'userName',title:'名称',width:200}
            ]
          }"
          @change="changeData"
        />
      </c-list-item>
      <c-list-item label="变更后部门">
        {{ orgInfo }}
      </c-list-item>
      <c-list-item :span="2">
        <div class="btn-container">
          <c-button @click="cancelHandle">取消</c-button>
          <c-button type="primary" @click="confirmHandle">确认</c-button>
        </div>
      </c-list-item>
    </c-list>
  </c-dialog>
</template>
<script>
import { postSetSupervisor } from '@/api/decorate'
import { otherFiltersItem } from '@/utils'
import { getOrgByAccount } from '@/api/common'

export default {
  name: 'Supervision',
  props: {
    selData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      orgInfo: '',
      formData: {
        proSupervisorCode: '',
        proSupervisorName: ''
      },
      formRules: {
        proSupervisorCode: [{ required: true, message: '请选择变更后工程监理', trigger: 'change' }]
      }
    }
  },
  computed: {
    detail() {
      return this.selData
    }
    // filtersRaw() {
    //   if (this.detail.proSupervisorCode) {
    //     return [
    //       otherFiltersItem('account', this.detail.proSupervisorCode, '<>')
    //     ]
    //   }
    //   return []
    // }
  },
  methods: {
    changeData(data) {
      getOrgByAccount(data.account).then(res => {
        if (res.success === 1) {
          if (res.data.length !== 0) {
            const list = res.data.map(el => el.orgName)
            this.orgInfo = list.join(',')
          } else {
            this.orgInfo = ''
          }
        }
      })
    },
    openFun() {
      if (!this.singleJudge(this.selData, '变更工程监理', false)) {
        return false
      }
      const flag = this.detail.some(v => v.schedule === 'M')
      if (flag) {
        this.$message.warning('所选项包括【已终止】任务！')
        return false
      }
      const flag2 = this.detail.some(v => v.isEnd === '0' || v.isEnd === 0)
      if (flag2) {
        this.$message.warning('所选项包括【已完结】任务！')
        return false
      }
      const flag3 = this.detail.some(v => !v.proSupervisorCode)
      if (flag3) {
        this.$message.warning('所选项包括未指派工程监理！')
        return false
      }
      return true
    },
    ajaxJson(data) {
      return new Promise((resolve, reject) => {
        postSetSupervisor(data, 'change').then(res => {
          resolve(res)
        }).catch((error) => {
          reject(error)
        })
      })
    },
    async  confirmHandle() {
      const pass = await this.$refs.form.validate()
      if (!pass) return
      let ecdList = {}
      await this.$gsec('EXCEPT020').then(([cfg, ecd]) => {
        ecdList = ecd
      })
      this.detail.forEach(el => {
        const d = el
        ecdList.constCode = d.constCode
        ecdList.bizCode = d.decorationTaskCode
        ecdList.modelId = d.id
        ecdList.exceptionDesc = '变更工程监理'
        const t = this.$gsed()
        t.remark = ''
        t.exceptionDesc = '变更工程监理'
        ecdList.shopExceptionDetailDTOS = [t]
        const data = {
          exceptionDTO: ecdList,
          id: d.id,
          ...this.formData
        }
        this.ajaxJson(data).then((res) => {
        })
      })
      this.$nextTick(() => {
        this.$message.success('操作成功！')
        this.$emit('on-success')
        this.orgInfo = ''
        this.$refs.dialog.visible = false
      })
    },
    cancelHandle() {
      this.$refs.dialog.visible = false
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
